function meldcalc(unitchoice,scoring,bilirubin,inr,creatinine,dialysis){

var cr_conv=1;
var	bili_conv=1;
var score=0;


if (unitchoice) {
cr_conv = 1/88.4;
bili_conv = 1/17.1;
}

if (!unitchoice) {
bili_conv = 1;
cr_conv = 1;
}

bilirubin=bilirubin*bili_conv;
creatinine=creatinine*cr_conv;

//MELD Score = (0.957 * ln(Serum Cr) + 0.378 * ln(Serum Bilirubin) + 1.120 * ln(INR) + 0.643 ) * 10 
//MELD UNOS (1.12 * Math.log(INR)) + (0.378 * Math.log(Bili)) + (0.957 *Math.log(V25)) + 0.643
// where V25	=	Math.max(V24, (4 * V15 ));  v15=dialysed
// V24	=	Math.min(V23,4);
// V23	=	Math.max(creatinine,1);   
// V21	=	Math.max(INR,1); 	// Max INR Value Rule
// V22	=	Math.max(Bili,1); 	// Max Bili Value Rule
// 	UNIS MELD Score	=	(1.12 * Math.log(V21)) + (0.378 * Math.log(V22)) + (0.957 *Math.log(V25)) + 0.643



if (scoring==0){   //Original MELD
//If dialysed

if (dialysis==1){
score = 10 * ((.378 * Math.log(bilirubin)) + (1.12 * Math.log(inr)) + (.957 * Math.log(4)) + .643);
if (score < 6) {value = 6;}
}

//If not dialysed

if (dialysis==0){
score = 10 * ((.378 * Math.log(bilirubin)) + (1.12 * Math.log(inr)) + (.957 * Math.log(creatinine)) + .643);
if (score < 6) {score = 6;}
}
}

if (scoring==1){ //UNOS

var V23	=	Math.max(creatinine,1); 
var V22	=	Math.max(bilirubin,1);
var V21	=	Math.max(inr,1);
var V24	=	Math.min(V23,4); 
var V25	=	Math.max(V24, (4 * dialysis ));
score=(1.12 * Math.log(V21)) + (0.378 * Math.log(V22)) + (0.957 *Math.log(V25)) + 0.643;
score=Math.round(score * 10);

}


//round off
score=Math.round(score);		

//max meld = 40

if (score>40){
score=40;
}

//convert back creatinine
if (unitchoice) {
creatinine = creatinine*88.4;
}


return {
			 score:score,
			 creatinine:creatinine
	};		 
}


var interp="In interpreting the MELD Score in hospitalized patients, the 3 month mortality is:"+"<br />* 40 or more - 100% mortality"+"<br />* 30-39 - 83% mortality"+"<br />* 20-29 - 76% mortality" +"<br /> * 10-19 - 27% mortality"+"<br />* <10 - 4% mortality";


function ref(){
// display reference
 return "Kamath PS. A model to predict survival in patients with end-stage liver disease. Hepatology 2001 33(2):464-70";
}

function help() {
	return "Choosed either UNOS variant or original MELD score" + "<br />Dialysed = had dialysis at least twice in the past week"+"<br />The maximum score given for MELD is 40. All values higher than 40 are given a score of 40" +"<br />" +interp;
}
